aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/fund/[id]/withdraw/+page.server.ts
blob: 696c52fffdce470a5a96c39199ba66ae1cba931f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import getRates from "$lib/rates";
import { auth, fd, get, post } from "$lib/utils";
import { fail, redirect } from "@sveltejs/kit";

export async function load({ params, parent }) {
	const { user } = await parent();
	if (!user) redirect(307, "/register");
	const rates = await getRates();
	const rate = rates[user.currency];
	const fund = await get(`/fund/${params.id}`);
	const { amount: balance } = fund;
	return { ...params, balance, rate };
}

export const actions = {
	default: async ({ cookies, request }) => {
		const body = await fd(request);
		try {
			await post("/take", body, auth(cookies));
		} catch (e) {
			const { message } = e as Error;
			return fail(400, { message });
		}

		redirect(307, `/fund/${body.id}`);
	},
};